%% Data Logger 004 27/11/2012 BP
clear;
clf;

%% Define Serial Port Name
port = input ('Input Port Name  ','s');
log = input ('Input Log File Name  ','s');

%% Create & Open Serial Port
SER = serial(port,'Terminator','lf','baud',19200,'DataTerminalReady','off');
%%
fopen(SER);
fwrite(SER,240);

%% Create Log File

fID=fopen([log,'.csv'],'w+');

%% Read Data
vals=500;

L=zeros(vals,5);
X=1:vals;

current_row=1;

while 1
    l = fgetl(SER);             %read line from serial
    fprintf(fID,[l,'\n']);      % Write data directly to a file
    a = eval(['[',l,']']);      %extract data from string
    if(size(a,2)==5)
        L(current_row,:)=a;     %add data to new line in array
        
        % Get elements of L as a ring buffer
        R=[current_row+1:vals,1:current_row];
        
<<<<<<< .mine
        subplot(2,2,1);
        plot(X,L(R,2));
        ylim([0 1024]);
        title('Left Foot Force');
        subplot(2,2,2);
        plot(X,L(R,3));
        ylim([0 1024]);
        title('Right Foot Force');
        subplot(2,2,3);
        plot(X,L(R,4));
        ylim([0 1024]);
        title('Temperature 1');
        subplot(2,2,4);
        plot(X,L(R,5));
        ylim([0 1024]);         %Plot data on graph
        title('Temperature 2');
=======
        subplot(2,2,1);
        plot(X,L(R,2));
        ylim([0,1024]);
        subplot(2,2,2);
        plot(X,L(R,3));
        ylim([0,1024]);
        subplot(2,2,3);
        plot(X,L(R,4));
        ylim([0,1024]);
        subplot(2,2,4);
        plot(X,L(R,5));
        ylim([0,1024]);         %Plot data on graph
>>>>>>> .r66
        drawnow;                %force figure to redraw
        
        % Increment buffer position, loop back if at the end
        current_row=mod(current_row,vals)+1;
    end
end

%When end function, end and fclose(SER) to close serial port before
%clearing variables.